package com.itrident.data;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.intellij.ide.fileTemplates.impl.UrlUtil;
import com.intellij.openapi.ui.Messages;
import org.yaml.snakeyaml.Yaml;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cxx
 * @create 2020-11-30 21:14
 **/
public class DataSource {

    private static List<BeanData> beanDatas = new ArrayList<> ();

    private static DataSource dataSource = new DataSource();

    public static List<Path> getPaths(String id){
        Map<String, List<Path>> map = dataSource.getBeanDatas ().stream ().collect (Collectors.toMap (BeanData::getId, BeanData::getPaths));
        return map.get (id);
    }

    public static void initData(){
        Yaml yaml = new Yaml ();
        try {
            String text = UrlUtil.loadText (DataSource.class.getResource ("/datasource/datasource.yml"));
            Map<String,List> dataMap = yaml.load (text);
            Gson gson = new Gson ();
            List datas = dataMap.get ("beanDatas");
            beanDatas = gson.fromJson(gson.toJson (datas), new TypeToken<List<BeanData>> () {}.getType());

        } catch (Exception e) {
            Messages.showMessageDialog(e.getMessage (),"异常",Messages.getInformationIcon());
        }
    }
    public static List<BeanData> getBeanDatas() {
        return dataSource.beanDatas;
    }

    public void setBeanDatas(List<BeanData> beanDatas) {
        this.beanDatas = beanDatas;
    }
}
